package xyz.labmem.base.sys.util

import com.alibaba.fastjson.JSON.parseObject
import com.alibaba.fastjson.JSONObject
import org.springframework.data.redis.core.HashOperations
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.stereotype.Repository
import java.util.concurrent.TimeUnit


/**
 * @Author lty
 * @Date 2021/12/14 15:51
 */
@Repository
class RedisService(
    private var stringRedisTemplate: StringRedisTemplate
) {

    fun add(key: String, value: String, time: Int, timeUnit: TimeUnit) {
        stringRedisTemplate.opsForValue()[key, value, time.toLong()] = timeUnit
    }

    fun add(key: String, value: String) {
        stringRedisTemplate.opsForValue()[key] = value
    }


    fun put(hash: String, key: String, value: String) =
        stringRedisTemplate.opsForHash<Any, Any>().put(hash, key, value)


    fun multiGet(hash: String, list: List<*>): List<*> =
        stringRedisTemplate.opsForHash<Any, Any>().multiGet(hash, list)


    /**
     * 设置有效时间
     *
     * @param key Redis键
     * @param time 超时时间
     * @param timeUnit 时间单位
     * @return true=设置成功；false=设置失败
     */
    fun expire(key: String, time: Int, timeUnit: TimeUnit): Boolean =
        stringRedisTemplate.expire(key, time.toLong(), timeUnit)

    /**
     * 获得缓存的基本对象。
     *
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     */
    fun get(key: String): String? =
        stringRedisTemplate.opsForValue()[key]

    /**
     * 获得缓存的基本对象列表
     *
     * @param pattern 字符串前缀
     * @return 对象列表
     */
    fun keys(pattern: String): Set<String> =
        stringRedisTemplate.keys(pattern)


    fun delete(key: String) =
        stringRedisTemplate.opsForValue().operations.delete(key)


    fun isNotEmpty(key: String): Boolean =
        stringRedisTemplate.hasKey(key)

    fun opsForHash(): HashOperations<String, String, String> =
        stringRedisTemplate.opsForHash()

    /**
     * 缓存基本的对象，Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     */
    fun <T> setCacheObject(key: String?, value: T) {
        if (key != null) {
            stringRedisTemplate.opsForValue().set(key, value.toString())
        }
    }

    /**
     * 缓存基本的对象，Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     * @param timeout 时间
     * @param timeUnit 时间颗粒度
     */
    fun <T> setCacheObject(key: String?, value: T, timeout: Int, timeUnit: TimeUnit?) {
        if (key != null) {
            if (timeUnit != null) {
                stringRedisTemplate.opsForValue().set(key, value.toString(), timeout.toLong(), timeUnit)
            }
        }
    }


    /**
     * 获得缓存的list对象
     *
     * @param key 缓存的键值
     * @return 缓存键值对应的数据
     */
    fun <T> getCacheList(key: String): MutableList<String>? {

        return key?.let { stringRedisTemplate.opsForList().range(it, 0, -1) }
    }

    /**
     * 获得缓存的json对象。
     *
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     */
    fun getCacheObject(key: String): JSONObject? {
        return parseObject(get(key))
    }

}